cc.Class({
    extends: cc.ScrollView,
    properties: {
        allView: cc.Node,
        itemCount: 0,
        entityCount: 5,
    },

    onLoad: function () {
        if (this.allView && this.content) {
            this.bounceDuration = 0;
            this.offsetIndex = 0;
            this.childHeight = this.allView.height / this.entityCount;
        }
    },

    onEnable: function () {
        if (!CC_EDITOR) {
            this._registerEvent();
        }
        //  this._showScrollbar();
        if (this.allView && this.content) {
            this.__inited = false;
            this.lastDeltaMove = {
                x: 0,
                y: 0
            };
        }
    },

    _moveContent: function (deltaMove, canStartBounceBack) {
        var adjustedMove = this._flattenVectorByDirection(deltaMove);

        var newPosition = this.wrapContent(adjustedMove);

        this.setContentPosition(newPosition);

        var outOfBoundary = this._getHowMuchOutOfBoundary();
        this._updateScrollBar(outOfBoundary);

        if (this.elastic && canStartBounceBack) {
            this._startBounceBackIfNeeded();
        }

    },

    wrapContent: function (deltaMove) {
        if (this.vertical && this.horizontal) {
            return this.getContentPosition().add(deltaMove); //cc.pAdd(this.getContentPosition(), deltaMove);
        }
        if (!this.allView) {
            return this.getContentPosition().add(deltaMove); //cc.pAdd(this.getContentPosition(), deltaMove);
        }
        if (this.vertical) {
            if (deltaMove.y < 0) {
                if (this.itemCount >= 0 && this.offsetIndex - 1 < 0) {
                    if (this.wrapContentEndCallback && this.lastDeltaMove.y < 0) {
                        this.wrapContentEndCallback(deltaMove, this._getHowMuchOutOfBoundary());
                    }
                    this.lastDeltaMove = deltaMove;
                    return this.getContentPosition().add(deltaMove); //cc.pAdd(this.getContentPosition(), deltaMove);
                }
                var wrapContentTopY = this.allView.parent.convertToWorldSpaceAR(this.allView.position).y;
                var wrapContentBottomY = wrapContentTopY - this.allView.height;

                var child = this.content.children[this.content.children.length - 1];
                var pos = this.content.convertToWorldSpaceAR(child.position);

                if (pos.y - child.height * child.anchorY < wrapContentBottomY) {
                    this.offsetIndex--;
                    if (this.wrapContentCallback) {
                        this.wrapContentCallback(child, this.offsetIndex);
                    }

                    var move = cc.v2(0, child.height);
                    if (cc.sys.isNative) {
                        var children = this.content.children;
                        child.setPosition(children[0].getPosition());
                        for (var i = 0; i < children.length - 1; i++) {
                            children[i].setPosition(children[i].getPosition().add(move))
                        }
                    }
                    child.setSiblingIndex(0);

                    if (this._autoScrolling) {
                        this._autoScrollStartPosition = this._autoScrollStartPosition.add(move);
                    }
                    this.lastDeltaMove = deltaMove;
                    return this.getContentPosition().add(move);
                }
            } else if (deltaMove.y > 0) {
                if (this.itemCount >= 0 && this.content.children.length + this.offsetIndex >= this.itemCount) {
                    if (this.wrapContentEndCallback && this.lastDeltaMove.y > 0) {
                        this.wrapContentEndCallback(deltaMove, this._getHowMuchOutOfBoundary());
                    }
                    this.lastDeltaMove = deltaMove;
                    return this.getContentPosition().add(deltaMove);
                }
                var wrapContentTopY = this.allView.parent.convertToWorldSpaceAR(this.allView.position).y;

                var child = this.content.children[0];
                var pos = this.content.convertToWorldSpaceAR(child.position);
                if (child.height * child.anchorY + pos.y > wrapContentTopY) {
                    this.offsetIndex++;
                    var index = this.content.children.length - 1;
                    if (this.wrapContentCallback) {
                        this.wrapContentCallback(child, index + this.offsetIndex);
                    }

                    var move = cc.v2(0, -child.height);
                    if (cc.sys.isNative) {
                        var children = this.content.children;
                        child.setPosition(children[this.content.children.length - 1].getPosition());
                        for (var i = 1; i < children.length; i++) {
                            children[i].setPosition(children[i].getPosition().add(move))
                        }
                    }
                    child.setSiblingIndex(this.content.children.length);

                    if (this._autoScrolling) {
                        this._autoScrollStartPosition = this._autoScrollStartPosition.add(move);
                    }
                    this.lastDeltaMove = deltaMove;
                    return this.getContentPosition().add(move);
                }
            }
        }
        return this.getContentPosition().add(deltaMove);
    },
});